package com.ruoyi.iot.controller;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.iot.service.WeatherService;
import com.ruoyi.iot.vo.WeatherVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.List;

import static com.ruoyi.iot.constant.iotDataConstants.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/iot/weather")
@Api(tags = {"物联网数据服务接口"}, value = "气象站物联网数据服务")
public class WeatherController {

    private final WeatherService weatherService;

    @GetMapping
    @DataSource(value = DataSourceType.MASTER)
    @ApiOperation(value = "气象站数据获取接口", notes = "获取指定时间范围内全部数据; 用于前端->管理端->物联网设备管理->获取设备历史时序数据")
    public R<List<WeatherVO>> get(@RequestParam(name = "sensorId") @NotNull String sensorId,
                                  @RequestParam(name = "startTime") @NotNull Instant startTime,
                                  @RequestParam(name = "endTime") @NotNull Instant endTime) {

        return R.ok(weatherService.get(sensorId, startTime, endTime));

    }

    @GetMapping("new")
    @DataSource(value = DataSourceType.MASTER)
    @ApiOperation(value = "气象站数据获取接口", notes = "获取最新一条数据; 用于前端->管理端->气象站数据展示->设备最新数据")
    public R<WeatherVO> getNew(@RequestParam(name = "sensorId") @NotNull String sensorId) {

        return R.ok(weatherService.getNew(sensorId));
    }

    @GetMapping("day")
    @DataSource(value = DataSourceType.MASTER)
    @ApiOperation(value = "气象站数据获取接口", notes = "获取最近一周时间范围内的数据; 用于前端->大屏端->气象站数据展示->'一天'模块下折线图展示")
    public R<List<WeatherVO>> day(@RequestParam(name = "sensorId") @NotNull String sensorId,
                               @RequestParam(name = "localTime") @NotNull Instant localTime) {

        // 获取当前时间前一天的时间
        Instant startTime = localTime.atZone(ZoneOffset.UTC)
                .minusDays(1)
                .withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0).toInstant();


        return R.ok(weatherService.getRange(sensorId, DAY_TARGET_POINTS, startTime, localTime));
    }

    @GetMapping("weekend")
    @DataSource(value = DataSourceType.MASTER)
    @ApiOperation(value = "气象站数据获取接口", notes = "获取最近一周时间范围内的数据; 用于前端->大屏端->气象站数据展示->'一周'模块下折线图展示")
    public R<List<WeatherVO>> weekend(@RequestParam(name = "sensorId", required = false) @NotNull String sensorId,
                                   @RequestParam(name = "localTime", required = false) @NotNull Instant localTime) {

        // 获取当前时间前一周的时间
        Instant startTime = localTime.atZone(ZoneOffset.UTC)
                .minusDays(7)
                .withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0).toInstant();

        return R.ok(weatherService.getRange(sensorId, WEEK_TARGET_POINTS, startTime, localTime));
    }

    @GetMapping("month")
    @DataSource(value = DataSourceType.MASTER)
    @ApiOperation(value = "气象站数据获取接口", notes = "获取最近一月时间范围内的数据; 用于前端->大屏端->气象站数据展示->'一月'模块下折线图展示")
    public R<List<WeatherVO>> month(@RequestParam(name = "sensorId", required = false) @NotNull String sensorId,
                                 @RequestParam(name = "localTime", required = false) @NotNull Instant localTime) {

        // 获取当前时间前一周的时间
        Instant startTime = localTime.atZone(ZoneOffset.UTC)
                .minusMonths(1)
                .withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0).toInstant();

        return R.ok(weatherService.getRange(sensorId, MONTH_TARGET_POINTS, startTime, localTime));
    }
}
